<!DOCTYPE html><html lang="zh-Hans"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1"><meta name="description" content="umijs+dva"><meta name="keywords" content="umijs dva react"><meta name="author" content="晟松"><meta name="copyright" content="晟松"><title>umijs+dva | 晟松</title><link rel="shortcut icon" href="/logo.png"><link rel="stylesheet" href="/css/index.css?version=1.9.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/font-awesome@latest/css/font-awesome.min.css?version=1.9.0"><meta name="format-detection" content="telephone=no"><meta http-equiv="x-dns-prefetch-control" content="on"><link rel="dns-prefetch" href="https://cdn.jsdelivr.net"><link rel="dns-prefetch" href="https://hm.baidu.com"><script>var _hmt = _hmt || [];
(function() {
  var hm = document.createElement("script");
  hm.src = "https://hm.baidu.com/hm.js?c34d81d53341febe06a155941e2df16d";
  var s = document.getElementsByTagName("script")[0]; 
  s.parentNode.insertBefore(hm, s);
})();</script><meta http-equiv="Cache-Control" content="no-transform"><meta http-equiv="Cache-Control" content="no-siteapp"><script>var GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: {"path":"search.xml","languages":{"hits_empty":"找不到您查询的内容:${query}"}},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  hexoVersion: '5.4.0'
} </script><meta name="generator" content="Hexo 5.4.0"></head><body><i class="fa fa-arrow-right" id="toggle-sidebar" aria-hidden="true"></i><div id="sidebar" data-display="true"><div class="toggle-sidebar-info text-center"><span data-toggle="切换文章详情">切换站点概览</span><hr></div><div class="sidebar-toc"><div class="sidebar-toc__title">目录</div><div class="sidebar-toc__progress"><span class="progress-notice">你已经读了</span><span class="progress-num">0</span><span class="progress-percentage">%</span><div class="sidebar-toc__progress-bar"></div></div><div class="sidebar-toc__content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#umijs-dva"><span class="toc-number">1.</span> <span class="toc-text">umijs+dva</span></a><ol class="toc-child"><li class="toc-item toc-level-3"><a class="toc-link" href="#dvajs"><span class="toc-number">1.1.</span> <span class="toc-text">dvajs</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E6%B5%81%E5%9B%BE"><span class="toc-number">1.1.1.</span> <span class="toc-text">数据流图</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%A0%B8%E5%BF%83%E6%A6%82%E5%BF%B5"><span class="toc-number">1.1.2.</span> <span class="toc-text">核心概念</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#connect-%E6%96%B9%E6%B3%95"><span class="toc-number">1.1.3.</span> <span class="toc-text">connect 方法</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#dva-%E5%BA%94%E7%94%A8%E7%9A%84%E6%9C%80%E7%AE%80%E7%BB%93%E6%9E%84%EF%BC%88%E5%B8%A6-model"><span class="toc-number">1.1.4.</span> <span class="toc-text">dva 应用的最简结构（带 model)</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%80%BB%E7%BB%93"><span class="toc-number">1.1.5.</span> <span class="toc-text">总结</span></a></li></ol></li><li class="toc-item toc-level-3"><a class="toc-link" href="#umijs"><span class="toc-number">1.2.</span> <span class="toc-text">umijs</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84"><span class="toc-number">1.2.1.</span> <span class="toc-text">目录结构</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#umirc-ts"><span class="toc-number">1.2.1.1.</span> <span class="toc-text">.umirc.ts</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#env"><span class="toc-number">1.2.1.2.</span> <span class="toc-text">.env</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#layouts-index-tsx"><span class="toc-number">1.2.1.3.</span> <span class="toc-text">layouts&#x2F;index.tsx</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#pages-%E7%9B%AE%E5%BD%95"><span class="toc-number">1.2.1.4.</span> <span class="toc-text">pages 目录</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#app-ts"><span class="toc-number">1.2.1.5.</span> <span class="toc-text">app.ts</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E6%96%87%E4%BB%B6"><span class="toc-number">1.2.2.</span> <span class="toc-text">配置文件</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%85%8D%E7%BD%AE%E5%BC%8F%E8%B7%AF%E7%94%B1"><span class="toc-number">1.2.3.</span> <span class="toc-text">配置式路由</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%9F%BA%E7%A1%80%E7%BB%93%E6%9E%84"><span class="toc-number">1.2.3.1.</span> <span class="toc-text">基础结构</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E9%A1%B5%E9%9D%A2%E8%B7%B3%E8%BD%AC"><span class="toc-number">1.2.3.2.</span> <span class="toc-text">页面跳转</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E8%B7%AF%E7%94%B1%E7%BB%84%E4%BB%B6%E5%8F%82%E6%95%B0"><span class="toc-number">1.2.3.3.</span> <span class="toc-text">路由组件参数</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%BA%A6%E5%AE%9A%E5%BC%8F%E8%B7%AF%E7%94%B1"><span class="toc-number">1.2.4.</span> <span class="toc-text">约定式路由</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8F%92%E4%BB%B6"><span class="toc-number">1.2.5.</span> <span class="toc-text">插件</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%90%AF%E7%94%A8%E6%8F%92%E4%BB%B6"><span class="toc-number">1.2.5.1.</span> <span class="toc-text">启用插件</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E6%A3%80%E6%9F%A5%E6%8F%92%E4%BB%B6%E5%90%AF%E7%94%A8%E6%83%85%E5%86%B5"><span class="toc-number">1.2.5.2.</span> <span class="toc-text">检查插件启用情况</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#%E5%91%BD%E4%BB%A4%E8%A1%8C"><span class="toc-number">1.2.5.2.1.</span> <span class="toc-text">命令行</span></a></li></ol></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#mock%E6%95%B0%E6%8D%AE"><span class="toc-number">1.2.6.</span> <span class="toc-text">mock数据</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%BA%A6%E5%AE%9A%E5%BC%8F-Mock-%E6%96%87%E4%BB%B6"><span class="toc-number">1.2.6.1.</span> <span class="toc-text">约定式 Mock 文件</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%BC%96%E5%86%99-Mock-%E6%96%87%E4%BB%B6"><span class="toc-number">1.2.6.2.</span> <span class="toc-text">编写 Mock 文件</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E7%8E%AF%E5%A2%83%E5%8F%98%E9%87%8F"><span class="toc-number">1.2.7.</span> <span class="toc-text">环境变量</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E4%BD%BF%E7%94%A8css"><span class="toc-number">1.2.8.</span> <span class="toc-text">使用css</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#%E5%85%A8%E5%B1%80%E6%A0%B7%E5%BC%8F"><span class="toc-number">1.2.8.1.</span> <span class="toc-text">全局样式</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#CSS-Modules"><span class="toc-number">1.2.8.2.</span> <span class="toc-text">CSS Modules</span></a></li></ol></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E6%8C%89%E9%9C%80%E5%8A%A0%E8%BD%BD"><span class="toc-number">1.2.9.</span> <span class="toc-text">按需加载</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#%E9%87%8D%E7%82%B9-%E6%8F%92%E4%BB%B6"><span class="toc-number">1.2.10.</span> <span class="toc-text">重点-插件</span></a><ol class="toc-child"><li class="toc-item toc-level-5"><a class="toc-link" href="#umijs-preset-react"><span class="toc-number">1.2.10.1.</span> <span class="toc-text">@umijs&#x2F;preset-react</span></a></li><li class="toc-item toc-level-5"><a class="toc-link" href="#%E7%AE%80%E5%8D%95%E7%9C%8B%E4%B8%8Bdva%E8%BF%99%E4%B8%AA%E6%8F%92%E4%BB%B6"><span class="toc-number">1.2.10.2.</span> <span class="toc-text">简单看下dva这个插件</span></a><ol class="toc-child"><li class="toc-item toc-level-6"><a class="toc-link" href="#model-%E7%94%A8%E4%BE%8B"><span class="toc-number">1.2.10.2.1.</span> <span class="toc-text">model 用例</span></a></li></ol></li></ol></li></ol></li></ol></li></ol></div></div><div class="author-info hide"><div class="author-info__avatar text-center"><img src="/img/avatar.png"></div><div class="author-info__name text-center">晟松</div><div class="author-info__description text-center">晟松的博客</div><div class="follow-button"><a target="_blank" rel="noopener" href="https://gitee.com/cheng_song">Gitee</a></div><hr><div class="author-info-articles"><a class="author-info-articles__archives article-meta" href="/archives"><span class="pull-left">文章</span><span class="pull-right">52</span></a><a class="author-info-articles__tags article-meta" href="/tags"><span class="pull-left">标签</span><span class="pull-right">45</span></a></div></div></div><div id="content-outer"><div id="top-container" style="background-image: url(/img/blog-bg.jpg)"><div id="page-header"><span class="pull-left"> <a id="site-name" href="/">晟松</a></span><i class="fa fa-bars toggle-menu pull-right" aria-hidden="true"></i><span class="pull-right menus">   <a class="site-page" href="/">Home</a><a class="site-page" href="/archives">Archives</a><a class="site-page" href="/tags">Tags</a><a class="site-page" href="/categories">Categories</a></span><span class="pull-right"><a class="site-page social-icon search"><i class="fa fa-search"></i><span> 搜索</span></a></span></div><div id="post-info"><div id="post-title">umijs+dva</div><div id="post-meta"><time class="post-meta__date"><i class="fa fa-calendar" aria-hidden="true"></i> 2021-11-19</time><div class="post-meta-wordcount"><span>字数总计: </span><span class="word-count">2.9k</span><span class="post-meta__separator">|</span><span>阅读时长: 12 分钟</span></div></div></div></div><div class="layout" id="content-inner"><article id="post"><div class="article-container" id="post-content"><h2 id="umijs-dva"><a href="#umijs-dva" class="headerlink" title="umijs+dva"></a>umijs+dva</h2><p>虽然react这块常见的框架我似乎也只知道nextjs这一个框架（我实习这公司是配合mobx响应数据流进行开发的），然后也简单的看了了下文档，但总感觉有点不太符合习惯。可能如果是为了ssr的话，选择这个没问题。</p>
<p>那个时候也看了下react-router，redux这些东西，路由这个还好，redux这个东西的api操作就感觉极其复杂（对，就是state的更新），虽然后面又看了下redux-toolkit这个帮助解决redux操作复杂的库？但感觉还是没有vuex简洁。</p>
<p>后面看到了umijs这个东西，感觉更符合国内的开发习惯，然后又看到了dvajs这个东西来更简单的操作数据流，所以就简单学一下。</p>
<h3 id="dvajs"><a href="#dvajs" class="headerlink" title="dvajs"></a><a target="_blank" rel="noopener" href="https://dvajs.com/guide/getting-started.html#%E5%AE%89%E8%A3%85-dva-cli">dvajs</a></h3><p>先单着看一下</p>
<h4 id="数据流图"><a href="#数据流图" class="headerlink" title="数据流图"></a>数据流图</h4><img src="https://zos.alipayobjects.com/rmsportal/hUFIivoOFjVmwNXjjfPE.png" alt="img" style="zoom: 50%;" />

<h4 id="核心概念"><a href="#核心概念" class="headerlink" title="核心概念"></a>核心概念</h4><ul>
<li>State：一个对象，保存整个应用状态</li>
<li>View：React 组件构成的视图层</li>
<li>Action：一个对象，描述事件</li>
<li>connect 方法：一个函数，绑定 State 到 View</li>
<li>dispatch 方法：一个函数，发送 Action 到 State</li>
</ul>
<h4 id="connect-方法"><a href="#connect-方法" class="headerlink" title="connect 方法"></a>connect 方法</h4><p>connect 是一个函数，绑定 State 到 View。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; connect &#125; <span class="keyword">from</span> <span class="string">&#x27;dva&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="function"><span class="keyword">function</span> <span class="title">mapStateToProps</span>(<span class="params">state</span>) </span>&#123;</span><br><span class="line">  <span class="keyword">return</span> &#123; <span class="attr">todos</span>: state.todos &#125;;</span><br><span class="line">&#125;</span><br><span class="line">connect(mapStateToProps)(App);</span><br></pre></td></tr></table></figure>

<p>connect 方法返回的也是一个 React 组件，通常称为容器组件。因为它是原始 UI 组件的容器，即在外面包了一层 State。</p>
<p>connect 方法传入的第一个参数是 mapStateToProps 函数，mapStateToProps 函数会返回一个对象，用于建立 State 到 Props 的映射关系。还有第二个参数，是mapDispatchToProps</p>
<h4 id="dva-应用的最简结构（带-model"><a href="#dva-应用的最简结构（带-model" class="headerlink" title="dva 应用的最简结构（带 model)"></a>dva 应用的最简结构（带 model)</h4><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// 创建应用</span></span><br><span class="line"><span class="keyword">const</span> app = dva();</span><br><span class="line"></span><br><span class="line"><span class="comment">// 注册 Model</span></span><br><span class="line">app.model(&#123;</span><br><span class="line">  <span class="attr">namespace</span>: <span class="string">&#x27;count&#x27;</span>,</span><br><span class="line">  <span class="attr">state</span>: &#123;</span><br><span class="line">    <span class="attr">record</span>: <span class="number">0</span>,</span><br><span class="line">    <span class="attr">current</span>: <span class="number">0</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">reducers</span>: &#123;</span><br><span class="line">    <span class="function"><span class="title">add</span>(<span class="params">state</span>)</span> &#123;</span><br><span class="line">      <span class="keyword">const</span> newCurrent = state.current + <span class="number">1</span>;</span><br><span class="line">      <span class="keyword">return</span> &#123; ...state,</span><br><span class="line">        <span class="attr">record</span>: newCurrent &gt; state.record ? newCurrent : state.record,</span><br><span class="line">        <span class="attr">current</span>: newCurrent,</span><br><span class="line">      &#125;;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="function"><span class="title">minus</span>(<span class="params">state</span>)</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> &#123; ...state, <span class="attr">current</span>: state.current - <span class="number">1</span>&#125;;</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">effects</span>: &#123;</span><br><span class="line">    *<span class="function"><span class="title">add</span>(<span class="params">action, &#123; call, put &#125;</span>)</span> &#123;</span><br><span class="line">      <span class="keyword">yield</span> call(delay, <span class="number">1000</span>);</span><br><span class="line">      <span class="keyword">yield</span> put(&#123; <span class="attr">type</span>: <span class="string">&#x27;minus&#x27;</span> &#125;);</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">subscriptions</span>: &#123;</span><br><span class="line">    <span class="function"><span class="title">keyboardWatcher</span>(<span class="params">&#123; dispatch &#125;</span>)</span> &#123;</span><br><span class="line">      key(<span class="string">&#x27;⌘+up, ctrl+up&#x27;</span>, <span class="function">() =&gt;</span> &#123; dispatch(&#123;<span class="attr">type</span>:<span class="string">&#x27;add&#x27;</span>&#125;) &#125;);</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 注册视图</span></span><br><span class="line">app.router(<span class="function">() =&gt;</span> <span class="xml"><span class="tag">&lt;<span class="name">ConnectedApp</span> /&gt;</span></span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 启动应用</span></span><br><span class="line">app.start(<span class="string">&#x27;#root&#x27;</span>);</span><br></pre></td></tr></table></figure>

<p>这effects异步操作直接原生generate吗？</p>
<p>dva 提供多个 effect 函数内部的处理函数，比较常用的是 <code>call</code> 和 <code>put</code>。</p>
<ul>
<li>call：执行异步函数</li>
<li>put：发出一个 Action，类似于 dispatch</li>
</ul>
<h4 id="总结"><a href="#总结" class="headerlink" title="总结"></a>总结</h4><p>这，好像也没简化多少（对比redux-toolkit），state的更新还是难受，connect依旧在这里，异步操作还是原生generate，看一下配合umijs如何简化吧</p>
<h3 id="umijs"><a href="#umijs" class="headerlink" title="umijs"></a><a target="_blank" rel="noopener" href="https://umijs.org/zh-CN/docs/getting-started">umijs</a></h3><p>虽然看着一些评价说这个比较适合入门使用，因为太重了，依赖太多了。但还是简单的看下吧。</p>
<p>额，yarn安装失败了</p>
<h4 id="目录结构"><a href="#目录结构" class="headerlink" title="目录结构"></a>目录结构</h4><p>一个基础的 Umi 项目大致是这样的，</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">.</span><br><span class="line">├── package.json</span><br><span class="line">├── .umirc.ts</span><br><span class="line">├── .env</span><br><span class="line">├── dist</span><br><span class="line">├── mock</span><br><span class="line">├── public</span><br><span class="line">└── src</span><br><span class="line">    ├── .umi</span><br><span class="line">    ├── layouts/index.tsx</span><br><span class="line">    ├── pages</span><br><span class="line">        ├── index.less</span><br><span class="line">        └── index.tsx</span><br><span class="line">    └── app.ts</span><br></pre></td></tr></table></figure>

<h5 id="umirc-ts"><a href="#umirc-ts" class="headerlink" title=".umirc.ts"></a>.umirc.ts</h5><p>配置文件，包含 umi 内置功能和插件的配置。</p>
<h5 id="env"><a href="#env" class="headerlink" title=".env"></a>.env</h5><p>环境变量。</p>
<p>比如：</p>
<figure class="highlight plaintext"><table><tr><td class="code"><pre><span class="line">PORT=8888</span><br><span class="line">COMPRESS=none</span><br></pre></td></tr></table></figure>

<h5 id="layouts-index-tsx"><a href="#layouts-index-tsx" class="headerlink" title="layouts/index.tsx"></a>layouts/index.tsx</h5><p>约定式路由时的全局布局文件。</p>
<h5 id="pages-目录"><a href="#pages-目录" class="headerlink" title="pages 目录"></a>pages 目录</h5><p>所有路由组件存放在这里。</p>
<h5 id="app-ts"><a href="#app-ts" class="headerlink" title="app.ts"></a>app.ts</h5><p>运行时配置文件，可以在这里扩展运行时的能力，比如修改路由、修改 render 方法等。</p>
<p>Umi 在 <code>.umirc.ts</code> 或 <code>config/config.ts</code> 中配置项目和插件，支持 es6。一份常见的配置如下，</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="built_in">export</span> default &#123;</span><br><span class="line">  base: <span class="string">&#x27;/docs/&#x27;</span>,</span><br><span class="line">  publicPath: <span class="string">&#x27;/static/&#x27;</span>,</span><br><span class="line">  <span class="built_in">hash</span>: <span class="literal">true</span>,</span><br><span class="line">  <span class="built_in">history</span>: &#123;</span><br><span class="line">    <span class="built_in">type</span>: <span class="string">&#x27;hash&#x27;</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h4 id="配置文件"><a href="#配置文件" class="headerlink" title="配置文件"></a>配置文件</h4><p>Umi 在 <code>.umirc.ts</code> 或 <code>config/config.ts</code> 中配置项目和插件，支持 es6。一份常见的配置如下，</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line"><span class="built_in">export</span> default &#123;</span><br><span class="line">  base: <span class="string">&#x27;/docs/&#x27;</span>,</span><br><span class="line">  publicPath: <span class="string">&#x27;/static/&#x27;</span>,</span><br><span class="line">  <span class="built_in">hash</span>: <span class="literal">true</span>,</span><br><span class="line">  <span class="built_in">history</span>: &#123;</span><br><span class="line">    <span class="built_in">type</span>: <span class="string">&#x27;hash&#x27;</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>如果项目的配置不复杂，推荐在 <code>.umirc.ts</code> 中写配置； </p>
<p>如果项目的配置比较复杂，可以将配置写在 <code>config/config.ts</code> 中，并把配置的一部分拆分出去，比如路由配置可以拆分成单独的 <code>routes.ts</code>：</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="comment">// config/routes.ts</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> [</span><br><span class="line">    &#123; <span class="attr">exact</span>: <span class="literal">true</span>, <span class="attr">path</span>: <span class="string">&#x27;/&#x27;</span>, <span class="attr">component</span>: <span class="string">&#x27;index&#x27;</span> &#125;,</span><br><span class="line">];</span><br></pre></td></tr></table></figure>

<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// config/config.ts</span></span><br><span class="line"></span><br><span class="line"><span class="keyword">import</span> &#123; defineConfig &#125; <span class="keyword">from</span> <span class="string">&#x27;umi&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> routes <span class="keyword">from</span> <span class="string">&#x27;./routes&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> defineConfig(&#123;</span><br><span class="line">  <span class="attr">routes</span>: routes,</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p><strong><code>.umirc.ts</code> 优先级更高</strong></p>
<p>同时配置还有本地配置、临时配置、多环境配置、运行时配置等</p>
<h4 id="配置式路由"><a href="#配置式路由" class="headerlink" title="配置式路由"></a>配置式路由</h4><h5 id="基础结构"><a href="#基础结构" class="headerlink" title="基础结构"></a>基础结构</h5><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="attr">routes</span>: [</span><br><span class="line">    &#123; <span class="attr">exact</span>: <span class="literal">true</span>, <span class="attr">path</span>: <span class="string">&#x27;/&#x27;</span>, <span class="attr">component</span>: <span class="string">&#x27;index&#x27;</span> &#125;,</span><br><span class="line">    &#123; <span class="attr">exact</span>: <span class="literal">true</span>, <span class="attr">path</span>: <span class="string">&#x27;/user&#x27;</span>, <span class="attr">component</span>: <span class="string">&#x27;user&#x27;</span> &#125;,</span><br><span class="line">  ],</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>exact：表示是否严格匹配，即 location 是否和 path 完全对应上。</p>
<p>routes：子路由</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="attr">routes</span>: [</span><br><span class="line">    &#123; <span class="attr">path</span>: <span class="string">&#x27;/login&#x27;</span>, <span class="attr">component</span>: <span class="string">&#x27;login&#x27;</span> &#125;,</span><br><span class="line">    &#123;</span><br><span class="line">      <span class="attr">path</span>: <span class="string">&#x27;/&#x27;</span>,</span><br><span class="line">      <span class="attr">component</span>: <span class="string">&#x27;@/layouts/index&#x27;</span>,</span><br><span class="line">      <span class="attr">routes</span>: [</span><br><span class="line">        &#123; <span class="attr">path</span>: <span class="string">&#x27;/list&#x27;</span>, <span class="attr">component</span>: <span class="string">&#x27;list&#x27;</span> &#125;,</span><br><span class="line">        &#123; <span class="attr">path</span>: <span class="string">&#x27;/admin&#x27;</span>, <span class="attr">component</span>: <span class="string">&#x27;admin&#x27;</span> &#125;,</span><br><span class="line">      ],</span><br><span class="line">    &#125;, </span><br><span class="line">  ],</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后在 <code>src/layouts/index</code> 中通过 <code>props.children</code> 渲染子路由，</p>
<figure class="highlight jsx"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> (props) =&gt; &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span> <span class="attr">style</span>=<span class="string">&#123;&#123;</span> <span class="attr">padding:</span> <span class="attr">20</span> &#125;&#125;&gt;</span>&#123; props.children &#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这样，访问 <code>/list</code> 和 <code>/admin</code> 就会带上 <code>src/layouts/index</code> 这个 layout 组件。</p>
<p>redirect：路由重定向</p>
<p>wrappers：配置路由的高阶组件封装</p>
<p>比如，可以用于路由级别的权限校验：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="attr">routes</span>: [</span><br><span class="line">    &#123; <span class="attr">path</span>: <span class="string">&#x27;/user&#x27;</span>, <span class="attr">component</span>: <span class="string">&#x27;user&#x27;</span>,</span><br><span class="line">      <span class="attr">wrappers</span>: [</span><br><span class="line">        <span class="string">&#x27;@/wrappers/auth&#x27;</span>,</span><br><span class="line">      ],</span><br><span class="line">    &#125;,</span><br><span class="line">    &#123; <span class="attr">path</span>: <span class="string">&#x27;/login&#x27;</span>, <span class="attr">component</span>: <span class="string">&#x27;login&#x27;</span> &#125;,</span><br><span class="line">  ]</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后在 <code>src/wrappers/auth</code> 中，</p>
<figure class="highlight jsx"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; Redirect &#125; <span class="keyword">from</span> <span class="string">&#x27;umi&#x27;</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> (props) =&gt; &#123;  </span><br><span class="line">    <span class="keyword">const</span> &#123; isLogin &#125; = useAuth();  </span><br><span class="line">    <span class="keyword">if</span> (isLogin) &#123;    </span><br><span class="line">        <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>&#123; props.children &#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>;  </span><br><span class="line">    &#125; </span><br><span class="line">    <span class="keyword">else</span> &#123;    </span><br><span class="line">        <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">Redirect</span> <span class="attr">to</span>=<span class="string">&quot;/login&quot;</span> /&gt;</span></span>; </span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>这样，访问 <code>/user</code>，就通过 <code>useAuth</code> 做权限校验，如果通过，渲染 <code>src/pages/user</code>，否则跳转到 <code>/login</code>，由 <code>src/pages/login</code> 进行渲染。</p>
<p>title：路由标题</p>
<h5 id="页面跳转"><a href="#页面跳转" class="headerlink" title="页面跳转"></a>页面跳转</h5><figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; history &#125; <span class="keyword">from</span> <span class="string">&#x27;umi&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 跳转到指定路由</span></span><br><span class="line">history.push(<span class="string">&#x27;/list&#x27;</span>);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 带参数跳转到指定路由</span></span><br><span class="line">history.push(<span class="string">&#x27;/list?a=b&#x27;</span>);</span><br><span class="line">history.push(&#123;</span><br><span class="line">  <span class="attr">pathname</span>: <span class="string">&#x27;/list&#x27;</span>,</span><br><span class="line">  <span class="attr">query</span>: &#123;</span><br><span class="line">    <span class="attr">a</span>: <span class="string">&#x27;b&#x27;</span>,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;);</span><br><span class="line"></span><br><span class="line"><span class="comment">// 跳转到上一个路由</span></span><br><span class="line">history.goBack();</span><br></pre></td></tr></table></figure>

<p>还有<strong>link组件</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; Link &#125; <span class="keyword">from</span> <span class="string">&#x27;umi&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> () =&gt; (</span><br><span class="line">  <span class="xml"><span class="tag">&lt;<span class="name">Link</span> <span class="attr">to</span>=<span class="string">&quot;/list&quot;</span>&gt;</span>Go to list page<span class="tag">&lt;/<span class="name">Link</span>&gt;</span></span></span><br><span class="line">);</span><br></pre></td></tr></table></figure>



<h5 id="路由组件参数"><a href="#路由组件参数" class="headerlink" title="路由组件参数"></a>路由组件参数</h5><p>路由组件可通过 <code>props</code> 获取到以下属性，</p>
<ul>
<li>match，当前路由和 url match 后的对象，包含 <code>params</code>、<code>path</code>、<code>url</code> 和 <code>isExact</code> 属性</li>
<li>location，表示应用当前处于哪个位置，包含 <code>pathname</code>、<code>search</code>、<code>query</code> 等属性</li>
<li>history，同 <a target="_blank" rel="noopener" href="https://umijs.org/zh-CN/api#history">api#history</a> 接口</li>
<li>route，当前路由配置，包含 <code>path</code>、<code>exact</code>、<code>component</code>、<code>routes</code> 等</li>
<li>routes，全部路由信息</li>
</ul>
<p>比如：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> <span class="function"><span class="keyword">function</span>(<span class="params">props</span>) </span>&#123;</span><br><span class="line">  <span class="built_in">console</span>.log(props.route);</span><br><span class="line">  <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>Home Page<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<h4 id="约定式路由"><a href="#约定式路由" class="headerlink" title="约定式路由"></a><a target="_blank" rel="noopener" href="https://umijs.org/zh-CN/docs/convention-routing">约定式路由</a></h4><p>就是直接通过目录结构和名字自动生成路由，nextjs和nuxtjs这些ssr的似乎都是这么做的</p>
<p>比如404页面，全局layout等，需要的时候再去看吧</p>
<h4 id="插件"><a href="#插件" class="headerlink" title="插件"></a>插件</h4><p>目前还没多大的感觉，先看着</p>
<p>插件有自己的命名规则，所以就有了ID和key。</p>
<p>比如插件 <code>/node_modules/@umijs/plugin-foo/index.js</code>，通常来说，其 id 为 <code>@umijs/plugin-foo</code>，key 为 <code>foo</code>。</p>
<h5 id="启用插件"><a href="#启用插件" class="headerlink" title="启用插件"></a>启用插件</h5><p>package.json里面引入umi的插件会自动启用。</p>
<p>在配置里可通过 <code>presets</code> 和 <code>plugins</code> 配置插件，比如：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="attr">presets</span>: [<span class="string">&#x27;./preset&#x27;</span>, <span class="string">&#x27;foo/presets&#x27;</span>],</span><br><span class="line">  <span class="attr">plugins</span>: [<span class="string">&#x27;./plugin&#x27;</span>],</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="检查插件启用情况"><a href="#检查插件启用情况" class="headerlink" title="检查插件启用情况"></a>检查插件启用情况</h5><h6 id="命令行"><a href="#命令行" class="headerlink" title="命令行"></a>命令行</h6><p>可以执行以下命令，</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">$ umi plugin list</span><br><span class="line"><span class="comment"># 顺便看看他们分别用了哪些 key</span></span><br><span class="line">$ umi plugin list --key</span><br></pre></td></tr></table></figure>



<h4 id="mock数据"><a href="#mock数据" class="headerlink" title="mock数据"></a><a target="_blank" rel="noopener" href="https://umijs.org/zh-CN/docs/mock">mock数据</a></h4><h5 id="约定式-Mock-文件"><a href="#约定式-Mock-文件" class="headerlink" title="约定式 Mock 文件"></a>约定式 Mock 文件</h5><p>Umi 约定 <code>/mock</code> 文件夹下所有文件为 mock 文件。</p>
<p>比如：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">.</span><br><span class="line">├── mock</span><br><span class="line">    ├── api.ts</span><br><span class="line">    └── users.ts</span><br><span class="line">└── src</span><br><span class="line">    └── pages</span><br><span class="line">        └── index.tsx</span><br></pre></td></tr></table></figure>

<p><code>/mock</code> 下的 <code>api.ts</code> 和 <code>users.ts</code> 会被解析为 mock 文件。</p>
<h5 id="编写-Mock-文件"><a href="#编写-Mock-文件" class="headerlink" title="编写 Mock 文件"></a>编写 Mock 文件</h5><p>如果 <code>/mock/api.ts</code> 的内容如下，</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="comment">// 支持值为 Object 和 Array</span></span><br><span class="line">  <span class="string">&#x27;GET /api/users&#x27;</span>: &#123; <span class="attr">users</span>: [<span class="number">1</span>, <span class="number">2</span>] &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="comment">// GET 可忽略</span></span><br><span class="line">  <span class="string">&#x27;/api/users/1&#x27;</span>: &#123; <span class="attr">id</span>: <span class="number">1</span> &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="comment">// 支持自定义函数，API 参考 express@4</span></span><br><span class="line">  <span class="string">&#x27;POST /api/users/create&#x27;</span>: <span class="function">(<span class="params">req, res</span>) =&gt;</span> &#123;</span><br><span class="line">    <span class="comment">// 添加跨域请求头</span></span><br><span class="line">    res.setHeader(<span class="string">&#x27;Access-Control-Allow-Origin&#x27;</span>, <span class="string">&#x27;*&#x27;</span>);</span><br><span class="line">    res.end(<span class="string">&#x27;ok&#x27;</span>);</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p>然后访问 <code>/api/users</code> 就能得到 <code>&#123; users: [1,2] &#125;</code> 的响应，其他以此类推。</p>
<p>还可以引入mockjs来加强</p>
<h4 id="环境变量"><a href="#环境变量" class="headerlink" title="环境变量"></a>环境变量</h4><p>一大堆，包括port端口，babel、hmr等</p>
<h4 id="使用css"><a href="#使用css" class="headerlink" title="使用css"></a>使用css</h4><p>默认支持less，</p>
<h5 id="全局样式"><a href="#全局样式" class="headerlink" title="全局样式"></a>全局样式</h5><p>Umi 中约定 <code>src/global.css</code> 为全局样式，如果存在此文件，会被自动引入到入口文件最前面。</p>
<p>比如用于覆盖样式，</p>
<figure class="highlight css"><table><tr><td class="code"><pre><span class="line"><span class="selector-class">.ant-select-selection</span> &#123;</span><br><span class="line">  <span class="attribute">max-height</span>: <span class="number">51px</span>;</span><br><span class="line">  <span class="attribute">overflow</span>: auto;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<h5 id="CSS-Modules"><a href="#CSS-Modules" class="headerlink" title="CSS Modules"></a>CSS Modules</h5><p>Umi 会自动识别 CSS Modules 的使用，你把他当做 CSS Modules 用时才是 CSS Modules。</p>
<p>比如：</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="comment">// CSS Modules</span></span><br><span class="line"><span class="keyword">import</span> styles <span class="keyword">from</span> <span class="string">&#x27;./foo.css&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 非 CSS Modules</span></span><br><span class="line"><span class="keyword">import</span> <span class="string">&#x27;./foo.css&#x27;</span>;</span><br></pre></td></tr></table></figure>



<h4 id="按需加载"><a href="#按需加载" class="headerlink" title="按需加载"></a><a target="_blank" rel="noopener" href="https://umijs.org/zh-CN/docs/load-on-demand">按需加载</a></h4><p><strong>常见使用场景</strong>：组件体积太大，不适合直接计入 bundle 中，以免影响首屏加载速度。例如：某组件 HugeA 包含巨大的实现 / 依赖了巨大的三方库，且该组件 HugeA 的使用不在首屏显示范围内，可被单独拆出。这时候，<code>dynamic</code> 就该上场了。</p>
<p>为了简化部署成本，按需加载功能默认是关闭的，你需要在使用之前先通过配置开启，</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> &#123;</span><br><span class="line">  <span class="attr">dynamicImport</span>: &#123;&#125;,</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<p><strong>封装一个异步组件</strong></p>
<p>通常搭配 <a target="_blank" rel="noopener" href="https://github.com/tc39/proposal-dynamic-import">动态 import 语法</a> 使用。</p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; dynamic &#125; <span class="keyword">from</span> <span class="string">&#x27;umi&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> dynamic(&#123;</span><br><span class="line">  <span class="attr">loader</span>: <span class="keyword">async</span> <span class="function"><span class="keyword">function</span>(<span class="params"></span>) </span>&#123;</span><br><span class="line">    <span class="comment">// 这里的注释 webpackChunkName 可以指导 webpack 将该组件 HugeA 以这个名字单独拆出去</span></span><br><span class="line">    <span class="keyword">const</span> &#123; <span class="attr">default</span>: HugeA &#125; = <span class="keyword">await</span> <span class="keyword">import</span>(<span class="comment">/* webpackChunkName: &quot;external_A&quot; */</span> <span class="string">&#x27;./HugeA&#x27;</span>);</span><br><span class="line">    <span class="keyword">return</span> HugeA;</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;);</span><br></pre></td></tr></table></figure>

<p><strong>使用异步组件</strong></p>
<figure class="highlight js"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> React <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> AsyncHugeA <span class="keyword">from</span> <span class="string">&#x27;./AsyncHugeA&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="comment">// 像使用普通组件一样即可</span></span><br><span class="line"><span class="comment">// dynamic 为你做:</span></span><br><span class="line"><span class="comment">// 1. 异步加载该模块的 bundle</span></span><br><span class="line"><span class="comment">// 2. 加载期间 显示 loading（可定制）</span></span><br><span class="line"><span class="comment">// 3. 异步组件加载完毕后，显示异步组件</span></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> () =&gt; &#123;</span><br><span class="line">  <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">AsyncHugeA</span> /&gt;</span></span>;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>



<p>后面还有一大块讲部署、服务端渲染的东西</p>
<h4 id="重点-插件"><a href="#重点-插件" class="headerlink" title="重点-插件"></a>重点-插件</h4><h5 id="umijs-preset-react"><a href="#umijs-preset-react" class="headerlink" title="@umijs/preset-react"></a>@umijs/preset-react</h5><p>针对 react 应用的插件集。</p>
<p>包含：</p>
<ul>
<li><a target="_blank" rel="noopener" href="https://umijs.org/zh-CN/plugins/plugin-access">plugin-access</a>，权限管理</li>
<li><a target="_blank" rel="noopener" href="https://umijs.org/zh-CN/plugins/plugin-analytics">plugin-analytics</a>，统计管理</li>
<li><a target="_blank" rel="noopener" href="https://umijs.org/zh-CN/plugins/plugin-antd">plugin-antd</a>，整合 antd UI 组件</li>
<li><a target="_blank" rel="noopener" href="https://umijs.org/zh-CN/plugins/plugin-crossorigin">plugin-crossorigin</a>，通常用于 JS 出错统计</li>
<li><a target="_blank" rel="noopener" href="https://umijs.org/zh-CN/plugins/plugin-dva">plugin-dva</a>，整合 dva</li>
<li><a target="_blank" rel="noopener" href="https://umijs.org/zh-CN/plugins/plugin-helmet">plugin-helmet</a>，整合 <a target="_blank" rel="noopener" href="https://github.com/nfl/react-helmet">react-helmet</a>，管理 HTML 文档标签（如标题、描述等）</li>
<li><a target="_blank" rel="noopener" href="https://umijs.org/zh-CN/plugins/plugin-initial-state">plugin-initial-state</a>，初始化数据管理</li>
<li><a target="_blank" rel="noopener" href="https://umijs.org/zh-CN/plugins/plugin-layout">plugin-layout</a>，配置启用 ant-design-pro 的布局</li>
<li><a target="_blank" rel="noopener" href="https://umijs.org/zh-CN/plugins/plugin-locale">plugin-locale</a>，国际化能力</li>
<li><a target="_blank" rel="noopener" href="https://umijs.org/zh-CN/plugins/plugin-model">plugin-model</a>，基于 hooks 的简易数据流</li>
<li><a target="_blank" rel="noopener" href="https://umijs.org/zh-CN/plugins/plugin-request">plugin-request</a>，基于 umi-request 和 umi-hooks 的请求方案</li>
</ul>
<h5 id="简单看下dva这个插件"><a href="#简单看下dva这个插件" class="headerlink" title="简单看下dva这个插件"></a>简单看下dva这个插件</h5><ul>
<li><strong>约定式的 model 组织方式</strong>，不用手动注册 model</li>
<li><strong>文件名即 namespace</strong>，model 内如果没有声明 namespace，会以文件名作为 namespace</li>
<li><strong>内置 dva-loading</strong>，直接 connect <code>loading</code> 字段使用即可</li>
<li><strong>支持 immer</strong>，通过配置 <code>immer</code> 开启</li>
</ul>
<p>符合以下规则的文件会被认为是 model 文件，</p>
<ul>
<li><code>src/models</code> 下的文件</li>
<li><code>src/pages</code> 下，子目录中 models 目录下的文件</li>
<li><code>src/pages</code> 下，所有 model.ts 文件(不区分任何字母大小写)</li>
</ul>
<p>比如：</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">+ src</span><br><span class="line">  + models/a.ts</span><br><span class="line">  + pages</span><br><span class="line">    + foo/models/b.ts</span><br><span class="line">    + bar/model.ts</span><br></pre></td></tr></table></figure>

<p>其中 <code>a.ts</code>，<code>b.ts</code> 和 <code>model.ts</code> 如果其内容是有效 dva model 写法，则会被认为是 model 文件。</p>
<h6 id="model-用例"><a href="#model-用例" class="headerlink" title="model 用例"></a>model 用例</h6><figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> &#123; Effect, ImmerReducer, Reducer, Subscription &#125; <span class="keyword">from</span> <span class="string">&#x27;umi&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">interface</span> IndexModelState &#123;</span><br><span class="line">  <span class="attr">name</span>: <span class="built_in">string</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">interface</span> IndexModelType &#123;</span><br><span class="line">  <span class="attr">namespace</span>: <span class="string">&#x27;index&#x27;</span>;</span><br><span class="line">  state: IndexModelState;</span><br><span class="line">  effects: &#123;</span><br><span class="line">    <span class="attr">query</span>: Effect;</span><br><span class="line">  &#125;;</span><br><span class="line">  reducers: &#123;</span><br><span class="line">    <span class="attr">save</span>: Reducer&lt;IndexModelState&gt;;</span><br><span class="line">    <span class="comment">// 启用 immer 之后</span></span><br><span class="line">    <span class="comment">// save: ImmerReducer&lt;IndexModelState&gt;;</span></span><br><span class="line">  &#125;;</span><br><span class="line">  subscriptions: &#123; <span class="attr">setup</span>: Subscription &#125;;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> IndexModel: IndexModelType = &#123;</span><br><span class="line">  <span class="attr">namespace</span>: <span class="string">&#x27;index&#x27;</span>,</span><br><span class="line"></span><br><span class="line">  <span class="attr">state</span>: &#123;</span><br><span class="line">    <span class="attr">name</span>: <span class="string">&#x27;&#x27;</span>,</span><br><span class="line">  &#125;,</span><br><span class="line"></span><br><span class="line">  <span class="attr">effects</span>: &#123;</span><br><span class="line">    *<span class="function"><span class="title">query</span>(<span class="params">&#123; payload &#125;, &#123; call, put &#125;</span>)</span> &#123;&#125;,</span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">reducers</span>: &#123;</span><br><span class="line">    <span class="function"><span class="title">save</span>(<span class="params">state, action</span>)</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> &#123;</span><br><span class="line">        ...state,</span><br><span class="line">        ...action.payload,</span><br><span class="line">      &#125;;</span><br><span class="line">    &#125;,</span><br><span class="line">    <span class="comment">// 启用 immer 之后</span></span><br><span class="line">    <span class="comment">// save(state, action) &#123;</span></span><br><span class="line">    <span class="comment">//   state.name = action.payload;</span></span><br><span class="line">    <span class="comment">// &#125;,</span></span><br><span class="line">  &#125;,</span><br><span class="line">  <span class="attr">subscriptions</span>: &#123;</span><br><span class="line">    <span class="function"><span class="title">setup</span>(<span class="params">&#123; dispatch, history &#125;</span>)</span> &#123;</span><br><span class="line">      <span class="keyword">return</span> history.listen(<span class="function">(<span class="params">&#123; pathname &#125;</span>) =&gt;</span> &#123;</span><br><span class="line">        <span class="keyword">if</span> (pathname === <span class="string">&#x27;/&#x27;</span>) &#123;</span><br><span class="line">          dispatch(&#123;</span><br><span class="line">            <span class="attr">type</span>: <span class="string">&#x27;query&#x27;</span>,</span><br><span class="line">          &#125;);</span><br><span class="line">        &#125;</span><br><span class="line">      &#125;);</span><br><span class="line">    &#125;,</span><br><span class="line">  &#125;,</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> IndexModel;</span><br></pre></td></tr></table></figure>

<p>page：</p>
<figure class="highlight typescript"><table><tr><td class="code"><pre><span class="line"><span class="keyword">import</span> React, &#123; FC &#125; <span class="keyword">from</span> <span class="string">&#x27;react&#x27;</span>;</span><br><span class="line"><span class="keyword">import</span> &#123; IndexModelState, ConnectProps, Loading, connect &#125; <span class="keyword">from</span> <span class="string">&#x27;umi&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">interface</span> PageProps <span class="keyword">extends</span> ConnectProps &#123;</span><br><span class="line">  <span class="attr">index</span>: IndexModelState;</span><br><span class="line">  loading: <span class="built_in">boolean</span>;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"><span class="keyword">const</span> IndexPage: FC&lt;PageProps&gt; = <span class="function">(<span class="params">&#123; index, dispatch &#125;</span>) =&gt;</span> &#123;</span><br><span class="line">  <span class="keyword">const</span> &#123; name &#125; = index;</span><br><span class="line">  <span class="keyword">return</span> <span class="xml"><span class="tag">&lt;<span class="name">div</span>&gt;</span>Hello &#123;name&#125;<span class="tag">&lt;/<span class="name">div</span>&gt;</span></span>;</span><br><span class="line">&#125;;</span><br><span class="line"></span><br><span class="line"><span class="keyword">export</span> <span class="keyword">default</span> connect(</span><br><span class="line">  <span class="function">(<span class="params">&#123; index, loading &#125;: &#123; index: IndexModelState; loading: Loading &#125;</span>) =&gt;</span> (&#123;</span><br><span class="line">    index,</span><br><span class="line">    <span class="attr">loading</span>: loading.models.index,</span><br><span class="line">  &#125;),</span><br><span class="line">)(IndexPage);</span><br></pre></td></tr></table></figure>

</div></article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">晟松</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://chengsong.info/2021/11/19/umijs+dva简单学习/">https://chengsong.info/2021/11/19/umijs+dva简单学习/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a target="_blank" rel="noopener" href="https://creativecommons.org/licenses/by-nc-sa/4.0/">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://chengsong.info">晟松</a>！</span></div></div><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/umijs-dva-react/">umijs dva react</a></div><nav id="pagination"><div class="prev-post pull-left"><a href="/2022/03/03/vue-loader%E7%AE%80%E5%8D%95%E5%AD%A6%E4%B9%A0/"><i class="fa fa-chevron-left">  </i><span>vue-loader简单学习</span></a></div><div class="next-post pull-right"><a href="/2021/11/19/react%E5%AD%A6%E4%B9%A0%E8%AE%B0%E5%BD%95/"><span>react简单学习记录</span><i class="fa fa-chevron-right"></i></a></div></nav></div></div><footer class="footer-bg" style="background-image: url(/img/blog-bg.jpg)"><div class="layout" id="footer"><div class="copyright">&copy;2020 - 2023 By 晟松</div><div class="framework-info"><span>驱动 - </span><a target="_blank" rel="noopener" href="http://hexo.io"><span>Hexo</span></a><span class="footer-separator">|</span><span>主题 - </span><a target="_blank" rel="noopener" href="https://github.com/Molunerfinn/hexo-theme-melody"><span>Melody</span></a></div><div class="icp"><a target="_blank" rel="noopener" href="http://www.beian.miit.gov.cn/"><span>湘ICP备2020021729号</span></a></div><div class="busuanzi"><script async src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><span id="busuanzi_container_page_pv"><i class="fa fa-file"></i><span id="busuanzi_value_page_pv"></span><span></span></span></div></div></footer><i class="fa fa-arrow-up" id="go-up" aria-hidden="true"></i><script src="https://cdn.jsdelivr.net/npm/animejs@latest/anime.min.js"></script><script src="https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-animate@latest/velocity.min.js"></script><script src="https://cdn.jsdelivr.net/npm/velocity-ui-pack@latest/velocity.ui.min.js"></script><script src="/js/utils.js?version=1.9.0"></script><script src="/js/fancybox.js?version=1.9.0"></script><script src="/js/sidebar.js?version=1.9.0"></script><script src="/js/copy.js?version=1.9.0"></script><script src="/js/fireworks.js?version=1.9.0"></script><script src="/js/transition.js?version=1.9.0"></script><script src="/js/scroll.js?version=1.9.0"></script><script src="/js/head.js?version=1.9.0"></script><script src="/js/search/local-search.js"></script><script>if(/Android|webOS|iPhone|iPod|iPad|BlackBerry/i.test(navigator.userAgent)) {
  $('#nav').addClass('is-mobile')
  $('footer').addClass('is-mobile')
  $('#top-container').addClass('is-mobile')
}</script><div class="search-dialog" id="local-search"><div class="search-dialog__title" id="local-search-title">本地搜索</div><div id="local-input-panel"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章"></div></div></div><hr><div id="local-search-results"><div id="local-hits"></div><div id="local-stats"><div class="local-search-stats__hr" id="hr"><span>由</span> <a target="_blank" rel="noopener" href="https://github.com/wzpan/hexo-generator-search" style="color:#49B1F5;">hexo-generator-search</a>
 <span>提供支持</span></div></div></div><span class="search-close-button"><i class="fa fa-times"></i></span></div><div class="search-mask"></div></body></html>